package com.example.app_mqttclient.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.app_mqttclient.R;
import com.example.app_mqttclient.obj.HealthData;
import com.example.app_mqttclient.obj.MqttData;
import com.example.app_mqttclient.obj.MyMqttClient;


public class BlockToysActivity extends Activity {

    private static final String TAG = "BlockToysActivity";

    private String pubTopic = null;
    private String subTopic = null;

    private int messageSpinnerSelectedNum = 0;
    private Spinner messageSpinner = null;
    private Spinner beepSpinner = null;

    public static HealthData myHealthData = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_block_toys);

        Intent intent = this.getIntent(); // 获取当前Intent
        Bundle bundle = intent.getExtras(); // 从Intent中获取携带数据的Bundle
        MqttData mqttData = (MqttData) bundle.getSerializable("mqttData"); // 从Bundle中获取数据 用key对应查找
        this.pubTopic = mqttData.getPublishTopic();
        this.subTopic = mqttData.getSubscribeTopic();

        messageSpinner = findViewById(R.id.block_toys_spinner_message);
        beepSpinner = findViewById(R.id.block_toys_spinner_beep);
    }

    /**
     * @brief   方块玩具操作页面的按键点击处理
     * @param   view: view对象
     * @retval  None
     */
    public void blockToysActivityOnClick(View view) {
        switch (view.getId())
        {
            case R.id.block_toys_btn_back:
                Log.i(TAG, "headbar back btn OnClick");
                this.finish(); // 结束当前页面
                break;
            case R.id.block_toys_btn_more:
                Log.i(TAG, "headbar more btn OnClick");
                break;
            case R.id.block_toys_btn_message_send:
                Log.i(TAG, "block toys message send btn OnClick");
                MyMqttClient.mqttPublish(this.subTopic, "PHRASES:" + (messageSpinner.getSelectedItemPosition() + 1));
                break;
            case R.id.block_toys_btn_beep_send:
                Log.i(TAG, "block toys beep send btn OnClick");
                MyMqttClient.mqttPublish(this.subTopic, "BEEP:" + (beepSpinner.getSelectedItemPosition() + 1));
                break;
            case R.id.block_toys_btn_get_health:
                Log.i(TAG, "block toys get health btn OnClick");
                MyMqttClient.mqttPublish(this.subTopic, "HEALTH");
                new Thread() {
                    @Override
                    public void run() {
//                        super.run();
                        if(myHealthData != null)
                        {
                            setTextHealthData(myHealthData);
                            myHealthData = null;
                            onStop();
                        }
                    }
                }.start();
                break;
            case R.id.block_toys_btn_analyze_health:
                Log.i(TAG, "block toys analyze health btn OnClick");

                break;
        }
    }

    private void setTextHealthData(HealthData healthData)
    {
        TextView xlText = findViewById(R.id.block_toys_text_xl);
        xlText.setText(healthData.getHeartRate());
        TextView xyText = findViewById(R.id.block_toys_text_xy);
        xyText.setText(healthData.getSpo());
        TextView wshText = findViewById(R.id.block_toys_text_wxh);
        wshText.setText(healthData.getBk());
        TextView szyText = findViewById(R.id.block_toys_text_szy);
        szyText.setText(healthData.getLowYa());
        TextView ssyText = findViewById(R.id.block_toys_text_ssy);
        ssyText.setText(healthData.getHighYa());
    }
}